package daosimpl;

import model.Alumno;
import model.Profesor;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;

import util.HibernateUtil;

import daos.AlumnoDAO;

public class AlumnoDAOImplH implements AlumnoDAO {
	private Transaction tx;
	
	@Override
	public Alumno ValidarAlumno(Alumno p) throws HibernateException {
		Alumno result = null;
		try {
			
			tx = HibernateUtil.session.beginTransaction();
						
			Query query = HibernateUtil.session
					.createQuery("FROM Alumno where alumnoId = '" + p.getAlumnoId()
							+  "'");
			result = (Alumno) query.list().get(0);
			
			if(!p.getPassword().equals(result.getPassword())){
				return null;
			}
			
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}
			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result;        
	}

}
